package com.hqhy.like.stock.dao.persist.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqhy.like.common.pojo.vo.PageData;
import com.hqhy.like.common.util.PageInfoToPageDataConverter;
import com.hqhy.like.stock.dao.persist.mapper.StockInMapper;
import com.hqhy.like.stock.dao.persist.repository.IStockInRepository;
import com.hqhy.like.stock.pojo.entity.StockIn;
import com.hqhy.like.stock.pojo.param.StockSearchParam;
import com.hqhy.like.stock.pojo.vo.StockInListItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Repository
public class StockInRepositoryImpl implements IStockInRepository {
    @Autowired
    private StockInMapper stockInMapper;

    @Override
    public int insert(StockIn stockIn) {
        return stockInMapper.insert(stockIn);
    }

    @Override
    public int deleteById(Long id) {
        return stockInMapper.deleteById(id);
    }

    @Override
    public int updateById(StockIn stockIn) {
        return stockInMapper.updateById(stockIn);
    }

    @Override
    public PageData<StockInListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询入库列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<StockInListItemVO> list = stockInMapper.list();
        PageInfo<StockInListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public StockInListItemVO selectStockInById(Long id) {
        return stockInMapper.selectStockInById(id);
    }

    @Override
    public PageData<StockInListItemVO> listForSearch(StockSearchParam stockInSearchParam) {
        log.debug("开始执行【查询入库列表】的数据访问，格式化前的参数：{}",stockInSearchParam);
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (stockInSearchParam.getStart()!=null&&stockInSearchParam.getEnd()!=null){
            stockInSearchParam.setStartTime(LocalDate.parse(stockInSearchParam.getStart(),pattern).atStartOfDay());
            stockInSearchParam.setEndTime(LocalDate.parse(stockInSearchParam.getEnd(),pattern).plusDays(1).atStartOfDay());
        }
        log.debug("开始执行【查询入库列表】的数据访问，格式化后的参数：{}",stockInSearchParam);
        PageHelper.startPage(stockInSearchParam.getPageNum(), stockInSearchParam.getPageSize());
        List<StockInListItemVO> list = stockInMapper.listForSearch(stockInSearchParam);
        PageInfo<StockInListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public List<StockInListItemVO> listStockInByIds(List<Long> ids) {
        return stockInMapper.listStockInByIds(ids);
    }

    @Override
    public StockInListItemVO getStockInById(Long id) {
        return stockInMapper.selectStockInById(id);
    }
}
